package com.nginx.web.util;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.net.URLDecoder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author fudon
 */
public class MybatisPlusAndGenerator {

    private String entityPath = "com.nginx.web";
    private String author = "lyc";
    private String dbUrl = "jdbc:sqlite:D:/Project/nginx-web/nginx-web.db";
    private String dbUserName = "";
    private String dbPassword = "";

    private String[] tables = new String[]{"conf_info","http_info","server_info","location_info","upstream_info","upstream_server"};

    public static void main(String[] args) {
        MybatisPlusAndGenerator generator = new MybatisPlusAndGenerator();
        generator.build(generator.getServicePath() , generator.getOutputCommonPath());
    }

    public String getServicePath(){
        return "\\java\\" + entityPath.replaceAll("\\.", "\\\\") + "\\";
    }

    public String getOutputCommonPath(){
        try {
            String path = MybatisPlusAndGenerator.class.getResource("/").getPath();
            return URLDecoder.decode(path, "UTF-8")
                    .substring(1, path.length() - 1)
                    .replace("target", "src").replace("classes", "main");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public void build(String servicePath ,String outputCommonPath) {
        // 创建AutoGenerator对象
        AutoGenerator mpg = new AutoGenerator();
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        // 设置输出的路径 项目的绝对路径地址
        gc.setOutputDir(outputCommonPath + "\\java");
        // 设置作者
        gc.setAuthor(author);
        gc.setOpen(false);
        // 生成列
        gc.setBaseColumnList(true);
        // 生成result map集合
        gc.setBaseResultMap(true);
        gc.setSwagger2(true);
        gc.setDateType(DateType.ONLY_DATE);
        gc.setFileOverride(true);
        // 实体属性 Swagger2 注解
        mpg.setGlobalConfig(gc);
        // 去掉Service接口的首字母I
        gc.setServiceName("%sService");

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        // 连接的url地址
        dsc.setUrl(dbUrl);
        // 设置驱动的名称
        dsc.setDriverName("org.sqlite.JDBC");
        // 设置mysql的用户名
        dsc.setUsername(dbUserName);
        // 设置mysql的密码
        dsc.setPassword(dbPassword);
        // 设置自动生成器的数据源
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        // 设置包名
        pc.setParent(entityPath);
        Map<String, String> pathInfo = new HashMap<>(10);
        pathInfo.put(ConstVal.SERVICE_PATH, outputCommonPath + servicePath + "service");
        pathInfo.put(ConstVal.SERVICE_IMPL_PATH, outputCommonPath + servicePath + "service\\impl");
        pathInfo.put(ConstVal.ENTITY_PATH, outputCommonPath + servicePath + "entity");
        pathInfo.put(ConstVal.MAPPER_PATH, outputCommonPath + servicePath + "mapper");

        pathInfo.put(ConstVal.XML_PATH, outputCommonPath + "\\resources\\mybatis");
        pc.setPathInfo(pathInfo);
        // 设置自动生成器的包
        mpg.setPackageInfo(pc);
        // 生成策略的配置
        StrategyConfig strategyConfig = new StrategyConfig();
        // 生成指定表
        strategyConfig.setInclude(tables);
//        strategyConfig.setInclude("*");
        // 驼峰命名
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setLogicDeleteFieldName("bool_del");
        // 字段驼峰命名
//        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        // 设置实体Bean的lombok
        strategyConfig.setEntityLombokModel(true);
        strategyConfig.setEntityTableFieldAnnotationEnable(true);
        strategyConfig.setTableFillList(Arrays.asList(
                new TableFill("create_time", FieldFill.INSERT),
                new TableFill("update_time", FieldFill.UPDATE),
                new TableFill("create_by", FieldFill.INSERT),
                new TableFill("update_by", FieldFill.UPDATE)
        ));
        // 设置生成策略
        mpg.setStrategy(strategyConfig);
        TemplateConfig templateConfig = new TemplateConfig();
        //关闭controller层生成
        templateConfig.setController(null);
        templateConfig.setService(null);
        templateConfig.setMapper(null);
        templateConfig.setServiceImpl(null);
//        templateConfig.setEntity(null);
        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);
        // 执行自动生成器
        mpg.execute();
    }


}


